<?php
namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;
use app\common\model\ManagerRole as ManagerRoleModel;

class Manager extends Model
{
    //使用软删除
    use SoftDelete;

    //指定软删除的字段
    protected $deleteTime = 'deleted_time';

    //指定主键
    protected $pk = 'manager_id';

    //定义模型关联的表名
    protected $table = 'managers';

    //自定义初始化
    protected static function init()
    {

    }

    //关联manager_roles
    public function roles()
    {
        return $this->morphMany('ManagerRole', 'type', '');
    }


    public function getRoleIdsAttr($value, $data)
    {
        $temp = ManagerRoleModel::where('manager_id','=', $data['manager_id'])->where('module_name', '=', 'manage')->column('role_id');

        return $temp;
    }

    public function getIsActiveLabelAttr($value, $data)
    {
        $is_active = [
            0   => '未激活',
            1   => '已激活',
        ];
        return $is_active[$data['is_active']];
    }

    public function getIsSystemLabelAttr($value, $data)
    {
        $is_system = [
            0   => '否',
            1   => '是'
        ];

        return $is_system[$data['is_system']];
    }

    public function getLastLoginTimeLabelAttr($value, $data)
    {
        if (empty($data['last_login_time'])) {
            return '暂未登录';
        }
        return date('Y-m-d H:i:s', $data['last_login_time']);
    }

    public function getUpdatedTimeLabelAttr($value, $data)
    {
        if (empty($data['updated_time'])) {
            return '暂未修改';
        }
        return date('Y-m-d H:i:s', $data['updated_time']);
    }

    public function getLastLoginIpLabelAttr($value, $data)
    {
        if (empty($data['last_login_ip'])) {
            return '';
        }

        return long2ip(intval($data['last_login_ip']));
    }


}